package com.platform.controller;

import java.util.List;
import java.util.Map;

import com.platform.entity.UserEntity;
import com.platform.service.ResourceService;
import com.platform.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.entity.ApiResourceEntity;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 图片管理Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-03 23:35:12
 */
@Controller
@RequestMapping("resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserService userService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("resource:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ApiResourceEntity> apiResourceList = resourceService.queryList(query);

        for (ApiResourceEntity info : apiResourceList) {
            UserEntity userEntity = userService.queryObject(info.getMouldId());
            if (userEntity != null) {
                info.setUsername(userEntity.getNickname());
            } else {
                info.setUsername("--");
            }

        }

        int total = resourceService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apiResourceList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("resource:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        ApiResourceEntity apiResource = resourceService.queryObject(id);

        return R.ok().put("apiResource", apiResource);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("resource:save")
    @ResponseBody
    public R save(@RequestBody ApiResourceEntity apiResource) {
        resourceService.save(apiResource);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("resource:update")
    @ResponseBody
    public R update(@RequestBody ApiResourceEntity apiResource) {
        resourceService.update(apiResource);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/unShow")
    @RequiresPermissions("resource:delete")
    @ResponseBody
    public R unShow(@RequestBody Long id) {
        resourceService.unShow(id);
        return R.ok();
    }

    @RequestMapping("/isShow")
    @RequiresPermissions("resource:update")
    @ResponseBody
    public R isShow(@RequestBody Long id) {

        resourceService.isShow(id);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ApiResourceEntity> list = resourceService.queryList(params);

        return R.ok().put("list", list);
    }
}
